#include "stdafx.h"

//void thread(void*);

GUI::GUI()
{
    buttons.push_back(Button("Save", 40, glutGet(GLUT_WINDOW_HEIGHT) - 40, 100, 25, BLUE));
    buttons.push_back(Button("Load", 150, glutGet(GLUT_WINDOW_HEIGHT) - 40, 100, 25, BLUE));
    buttons.push_back(Button("Pen", 260, glutGet(GLUT_WINDOW_HEIGHT) - 40, 100, 25, BLUE));
    buttons.push_back(Button("Charc", 370, glutGet(GLUT_WINDOW_HEIGHT) - 40, 100, 25, BLUE));

    brush = THIN;

    //_beginthread(thread, 1, 0);

    Update();
}

void GUI::Render()
{
    for (unsigned int i = 0; i < buttons.size(); i++)
	{
        RenderButton(buttons[i].x, buttons[i].y, buttons[i].width, buttons[i].height, buttons[i].color, buttons[i].text);
	}	
	
}

bool GUI::Update()
{
    const char *buttonClick = 0;
    POINT mPos = mouse();

    for (unsigned int i = 0; i < buttons.size(); i++)
    {
        buttons[i].y = BUTTON_Y_ANCHOR;
        buttons[i].x = BUTTON_X_ANCHOR + (i * 110);

        if (mPos.x > buttons[i].x && mPos.x < buttons[i].x + buttons[i].width &&
            mPos.y > buttons[i].y && mPos.y < buttons[i].y + buttons[i].height)
        {
            buttons[i].color = GREEN;

            if (cdBuffer.button == 0) // Left click down
            {
                buttons[i].y = BUTTON_Y_ANCHOR + 3;
                buttons[i].x = BUTTON_X_ANCHOR + (i * 110) + 3;
                buttonClick = buttons[i].text.c_str();
            }
        }
        else
            buttons[i].color = BLUE;
    }

    if (cdBuffer.state == 1) // Clear on let go
    {
		pad->ClipBreaker();
        ButtonPress(buttonClick);
        ClearClickData();
    }

    if (cdBuffer.button == 0 && !buttonClick) // Left click down
    {
        pad->InsertStroke(mPos, brush);
    }

    if (cdBuffer.button == 2 && !buttonClick) // Right click down
    {
        pad->RemoveStroke(mPos);
    }

    return buttonClick == 0;
}

void GUI::ButtonPress(const char *name)
{
    if (name == 0)
        return;

    if (strcmp(name, "Save") == 0)
    {        
        ClearClickData();
        pad->Save();
    }
    else if (strcmp(name, "Load") == 0)
    {
        ClearClickData();
        pad->Load();
    }
    else if (strcmp(name, "Thin") == 0)
    {
        brush = THIN;
    }
    else if (strcmp(name, "Thick") == 0)
    {
        brush = THICK;
    }
}

void GUI::RenderButton(int x, int y, int width, int height, GLfloat *color, std::string text)
{
    renderer->RenderQuad(x+5, y+5, width, height, BLACK);
    renderer->RenderQuad(x, y, width, height, color);  
    renderer->PrintText(x + 30, y + 18, GLUT_BITMAP_HELVETICA_12, WHITE, text);
}

void GUI::ReceiveClickData(int button, int state, int x, int y)
{
    cdBuffer.button = button;
    cdBuffer.state = state;
    cdBuffer.x = x;
    cdBuffer.y = y;
}

void GUI::ClearClickData()
{
    cdBuffer.button = -1;
    cdBuffer.state = -1;
    cdBuffer.x = -1;
    cdBuffer.y = -1;
}

POINT GUI::mouse()
{
    HWND window = FindWindow(TEXT("FREEGLUT"), TEXT(PROGRAM_NAME)); // pointer to window

    POINT p;
    GetCursorPos(&p);
    ScreenToClient(window, &p);

    if ((int)p.x < 0)
        p.x = 0;

    if ((int)p.y < 0)
        p.y = 0;

    return p;
}

/*HHOOK g_hMouseHook;
 
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode >= 0)
    {
        PMSLLHOOKSTRUCT pmll = (PMSLLHOOKSTRUCT) lParam;
 
		HWND window = FindWindow(TEXT("FREEGLUT"), TEXT(PROGRAM_NAME)); // pointer to window

		POINT pos;
		pos.x = pmll->pt.x; 
		pos.y = pmll->pt.y;
		ScreenToClient(window, &pos);

		printf("Mouse: %d, %d\n", pos.x, pos.y);

		if (gui->shouldInsert())
			pad->InsertStroke(pos, gui->getBrush());
    }
 
    return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam);
}

void thread(void *)
{
    MSG msg;
 
    g_hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0 );
 
    if (!g_hMouseHook) printf("err: %d\n", GetLastError());
 
    while ( GetMessage(&msg, NULL, 0, 0) )
    {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
    }
 
    UnhookWindowsHookEx(g_hMouseHook);
}*/